package com.mazaiting.instructions.math;

import com.mazaiting.instructions.base.BytecodeReader;
import com.mazaiting.instructions.base.Index8Instruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtLocalVars;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 给局部变量表中的int变量增加常量值，局部变量表索引和常量值都由指令的操作数提供
 * @author mazaiting
 * @since 2021/8/15
 */
public class IIncInstruction {

    @EqualsAndHashCode(callSuper = true)
    @Data
    public static class IInc extends Index8Instruction {

        /**
         * 操作数
         */
        private int num;

        @Override
        public void fetchOperands(BytecodeReader reader) {
            this.index = reader.readU1ToUnsignedByte();
            this.num = reader.readU1();
        }

        @Override
        public void execute(MtFrame frame) {
            MtLocalVars vars = frame.getLocalVars();
            int val = vars.getInt(index);
            val += num;
            vars.setInt(index, val);
        }
    }

}
